#include "bank.h"
#include <ctime>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

bank::bank(int num_of_pl)
{
	fund = 15140 - ((num_of_pl)* 1500);
	for (int i = 0; i < 40; i++)
	{
		mortgage_owner[i] = 0;
	}
}

char *bank::paying_rent(player &p1, player &p2, location &l, location loc_arr[])
{

	char str[1000];
	if (l.is_station)
	{
		int count = 0;
		if (p2.properties[2] == 1)count++;
		if (p2.properties[7] == 1)count++;
		if (p2.properties[12] == 1)count++;
		if (p2.properties[17] == 1)count++;
		if (p1.money < (l.rent*count))
		{
			p1.is_eliminated = 1;
			for (int x = 0; x < 20; x++)
			{
				if (p1.properties[x] == 1)
				{
					p1.properties[x] = 0;
					loc_arr[x].owner_code = -1;
					fund += loc_arr[x].price / 2;
				}
			}
			sprintf_s(str, " Player %d couldn't pay rent. To be eliminated next round", p1.player_code+1);
			return str;
		}
		p1.money -= (l.rent*count);
		p2.money += (l.rent*count);
		sprintf_s(str, " Player %d paid $%d station rent to Player %d.", p1.player_code+1, l.rent*count, p2.player_code+1);
		return str;
	}
	else if (l.is_utility)
	{
		if (p1.money < (l.rent*p1.move)) // TO CHECK IF HE DOESNT HAVE MONEY
		{
			p1.is_eliminated = 1;
			for (int x = 0; x < 20; x++)
			{
				if (p1.properties[x] == 1)
				{
					p1.properties[x] = 0;
					loc_arr[x].owner_code = -1;
					fund += loc_arr[x].price / 2;
				}
			}
			sprintf_s(str, "Player %d couldn't pay rent. To be eliminated next round", p1.player_code);
			return str;
		}
		p1.money -= (l.rent*p1.move);
		p2.money += (l.rent*p1.move);
		sprintf_s(str, " Player %d paid $%d utility rent to Player %d.", p1.player_code+1, l.rent*p1.move, p2.player_code+1);
		return str;
	}
	else
	{
		if (p1.money < (l.rent))
		{
			p1.is_eliminated = 1;
			for (int x = 0; x < 20; x++)
			{
				if (p1.properties[x] == 1)
				{
					p1.properties[x] = 0;
					loc_arr[x].owner_code = -1;
					fund += loc_arr[x].price / 2;
				}
			}
			sprintf_s(str, "Player %d couldn't pay rent. To be eliminated next round", p1.player_code);
			return str;
		}
		p1.money -= l.rent;
		p2.money += l.rent;
		//cout << "You paid " << l.rent << " to " << p2.player_code << endl;
		sprintf_s(str, ". Player %d paid $%d rent to Player %d.", p1.player_code+1, l.rent, p2.player_code+1);
		return str;
	}
	
}

int bank::buying_property(int i, int j, player &p, location &l)
{
	
				if (p.money >= l.price)
				{
					//cout << "Congratulations. You now own " << l.name << endl;
					p.money -= l.price;
					fund += l.price;
					p.properties[j] = 1;
					l.owner_code = i;
					cout << i << endl;
					return 1;
				}
				else
				{
					//cout << "Sowie. Not enough Money" << endl;
					return 0;
				}
}


/*void bank::buying_back(int player_code, int location_code, player &p, location &l)
{
	p.properties[location_code] = 1;
	p.money -= ((l.price / 20) + l.price/2);
	fund += ((l.price / 20) + l.price / 2);
}*/

/*void bank::mortgage(location loc_arr[], player pla_arr[], int current_player) //change decision system
{
	while (1) //mortgaging loop
	{
		int k;
		cout << "Mortgage, shall we?" << endl; //insert decision logic
		int property_existence_flag = 0;
		for (k = 1; k < 2; k++) //parameters need changing
		{
			if (pla_arr[current_player].properties[k] == 1)
			{
				cout << "Property code: " << loc_arr[k].location_code << " Property Name: " << loc_arr[k].name << "Mortgage price: " << loc_arr[k].price / 2 << endl;
				property_existence_flag++;
			}
		}
		if (property_existence_flag == 0)
		{
			cout << "You have no property. Can't mortgage\n";
			break;
		}
		int mortgaged_flag = 0;
		cout << "Enter property code to mortgage: " << endl;
		int loc_code;
		cin >> loc_code;
		for (k = 1; k < 2; k++) //parameter needs changing
		{
			if (loc_code == loc_arr[k].location_code && loc_arr[k].owner_code == current_player)
			{
				//mortgaging(current_player, k, pla_arr[current_player], loc_arr[k]);
				pla_arr[current_player].properties[k] = -1;
				pla_arr[current_player].money += (loc_arr[k].price / 2);
				fund -= (loc_arr[k].price / 2);
				cout << "You have now mortgaged " << loc_arr[k].name << endl;
				cout << pla_arr[current_player].money << " " << fund << " " << pla_arr[current_player].properties[k] << endl;
				mortgaged_flag++;
			}
		}
		if (mortgaged_flag == 1) // if user types correct prop_code
		{
			cout << "Do you want to mortgage again? ";
			char decision;
			cin >> decision;
			if (decision == 'N')break;
			else continue;
		}
		else // if user types wrong name
		{
			cout << "Oops. Wrong code. Try again" << endl;
		}
	}
}*/

/*void bank::buy_back(location loc_arr[], player pla_arr[], int current_player) //have to change decision system
{
	while (1)
	{
		int k;
		cout << "Buyback Property" << endl;
		int mortgage_existence_flag = 0;
		for (k = 1; k < 2; k++) //change parameters
		{
			if (pla_arr[current_player].properties[k] == -1)
			{
				cout << "Property code: " << loc_arr[k].location_code << " Property Name: " << loc_arr[k].name << "Mortgage price: " << loc_arr[k].price / 2 << endl;
				mortgage_existence_flag++;
			}
		}
		if (mortgage_existence_flag == 0)
		{
			cout << "You have no mortgaged property. Can't buyback";
			break;
		}
		int buy_back_flag = 0;
		cout << "Enter property code to buyback " << endl;

		int loc_code;
		cin >> loc_code;
		for (k = 1; k < 2; k++) //parameter needs changing
		{
			if (loc_code == loc_arr[k].location_code && loc_arr[k].owner_code == current_player)
			{
				//b.buying_back(i, k, pla_arr[current_player], loc_arr[k]);
				pla_arr[current_player].properties[k] = 1;
				pla_arr[current_player].money -= ((loc_arr[k].price / 20) + loc_arr[k].price / 2);
				fund += ((loc_arr[k].price / 20) + loc_arr[k].price / 2);
				cout << "You have now bought back " << loc_arr[k].name << endl;
				cout << pla_arr[current_player].money << " " << fund << " " << pla_arr[current_player].properties[k] << endl;
				buy_back_flag++;
			}
		}
		if (buy_back_flag == 1) // if user types correct name
		{
			cout << "Do you want to buyback again? ";
			char decision;
			cin >> decision;
			if (decision == 'N')break;
			else continue;
		}
		else // if user types wrong name
		{
			cout << "Oops. Wrong code. Try again" << endl;
		}
	}
}*/

bank::~bank()
{
}
